package cn.yougou.serviceImpl;

import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.dto.Query;
import cn.yougou.mapper.YgCommentMapper;
import cn.yougou.mapper.YgOrderMapper;
import cn.yougou.pojo.YgComment;
import cn.yougou.pojo.YgOrder;
import cn.yougou.service.admin.YgAdminCommentService;
import cn.yougou.vo.YgCommentVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class YgAdminCommentServiceImpl implements YgAdminCommentService{

    @Autowired
    private YgCommentMapper ygCommentMapper;

    @Autowired
    private YgOrderMapper ygOrderMapper;

    /**
     * 根据条件分页查找
     * @param query
     * @return
     */
    @Override
    public Dto findAllByQuery(Query query) {
        //1 计算当前从多少条展示
        query.setPage((query.getPage()-1)*query.getLimit());
        //2 评价总条数
        query.setInfoCount(ygCommentMapper.countAll(null));
        //3 逻辑删除默认值
        query.setDeleted(1);
        return DtoUtil.returnDataListSuccess(ygCommentMapper.findAllByQuery(query),query);
    }

    /**
     * 逻辑删除评价
     * @param ygComment
     * @return
     */
    @Override
    public Dto delete(YgComment ygComment) {
        //逻辑删除
        //1 更改评论表
        ygComment.setDeleted(0);
        int delInt = ygCommentMapper.delete(ygComment);
        //2 将订单表的绑定评价id重新设置为-1
        int upInt = ygOrderMapper.updateCommentId(ygComment.getId());
        if(delInt+upInt == 2){
            return DtoUtil.returnSuccess();
        }
        return DtoUtil.returnFail("删除失败","50001");
    }
}
